﻿using HZH_Controls;
using HZH_Controls.Forms;
using Petroleum.DB.Model;
using Petroleum.Model;
using Petroleum.Tool;
using Petroleum.UC;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Petroleum
{
    public partial class FormMain : FrmBase
    {
        public SQLiteHelper sqliteHelper = new SQLiteHelper(ResServer.DBURL);
        public List<SessionData> projects = new List<SessionData>();
        public FormMain()
        {
            InitializeComponent();
            this.FormClosing += FormMain_FormClosing;
            InitFormMove(this.topControl1);
        }
        private void tvMenu_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //panControl.Controls.Clear();
            string strName = e.Node.Text.Trim();
            this.topControl1.Title(strName);
            switch (strName)
            {
                case "Basic Information":
                    AddControl(new Step1(this));
                    break;
                case "Formation Pressure":
                    AddControl(new Step2FormationPressurePrediction(this));
                    break;
                case "Formation Pressure Img":
                    AddControl(new Step2Img(this));
                    break;
                case "Well Trajectory":
                    AddControl(new Step3Table(this));
                    break;
                case "Well Trajectory Img":
                    AddControl(new Step3Img(this));
                    break;
                case "Casing Program":
                    AddControl(new Step4(this));
                    break;
                case "Well Bore Quality":
                    AddControl(new Step4WellBoreQuality(this));
                    break;
                case "Casing Program Sketch":
                    AddControl(new Step5(this));
                    break;
                case "Drilling Team Select":
                    AddControl(new StepADrillingTeamSelect(this));
                    break;
                case "BHA Design":
                    AddControl(new Step6BHADesign(this));
                    break;
                case "Consumables Amount":
                    //AddControl(new Step4(this));
                    break;
                case "Drilling Fluid":
                    if (Session.CasingProgram.CasingList.Count == 0)
                    {
                        FrmDialog.ShowDialog(this, "请先填写套管信息");
                        return;
                    }
                    //Session.CasingProgram.CasingList = new List<Casing>()
                    //{
                    //    new Casing(){ SpudNo = "1st Spud", BitSize = "609.3" },
                    //    new Casing(){ SpudNo = "2nt Spud", BitSize = "107.4" },
                    //    new Casing(){ SpudNo = "3qt Spud", BitSize = "387.9" }
                    //};
                    AddControl(new Step7(this));
                    break;
                case "Cost Estimation":
                    AddControl(new Step8EstimatedMaterialConsumptionAndCostDesign(this));
                    break;
                case "Bit Types":
                    AddControl(new Step9BitTypesDesign(this));
                    break;
                //case "Requirement Params":
                //    AddControl(new Step10(this));
                //    break;
                //case "Requirement Params":
                //    AddControl(new Step10(this));
                //    break;
                case "Hydraulic Parameters":
                    AddControl(new Step10(this));
                    break;
                case "Well Controler":
                    AddControl(new Step11(this));
                    break;
                case "Casing String":
                    AddControl(new Step12(this));
                    break;
                case "Other design":
                    AddControl(new Step13(this));
                    break;
                case "Document Requirements":
                    AddControl(new DocumensRequirementSelect(this));
                    break;
                case "Drilling Progress Plan":
                    AddControl(new Step14(this));
                    break;
                //  Drilling Progress Plan
                //TreeNode RPNodes = new TreeNode("  Requirement Params ");
                //  RPNodes.Nodes.Add("Hydraulic Parameters");
                //  RPNodes.Nodes.Add("Well Controler");
                //  RPNodes.Nodes.Add("Casing String");
                //  this.treeView.tvMenu.Nodes.Add(RPNodes);

                //  TreeNode DPPNodes = new TreeNode("  Drilling Progress Plan");
                //case "Requirement Params":
                //    AddControl(new Step10(this));
                //    break;
                default:
                    AddControl(new IndexControl());
                    break;
            }
        }
        public void AddControl(Control c)
        {
            Control dq = null;
            foreach (Control item in panControl.Controls)
            {
                item.Visible = false;
                if (item.Name == c.Name)
                {
                    dq = c;
                    item.Visible = true;
                }
            }
            if (dq == null)
            {
                this.panControl.Controls.Add(c);
            }
            //this.panControl.Controls.Clear();
            //this.panControl.Controls.Add(c);
        }
        private void FormMain_Load(object sender, EventArgs e)
        {
            try
            {
                ControlHelper.FreezeControl(this, true);
                TreeNode tnForm = new TreeNode("  Basic Information(Begin)");
                tnForm.Nodes.Add("Basic Information");
                tnForm.Nodes.Add("Formation Pressure ");
                tnForm.Nodes.Add("Formation Pressure Img");
                this.treeView.tvMenu.Nodes.Add(tnForm);

                TreeNode WBDNodes = new TreeNode("  Well Bore Design");
                WBDNodes.Nodes.Add("Well Trajectory");
                WBDNodes.Nodes.Add("Well Trajectory Img");
                WBDNodes.Nodes.Add("Casing Program ");
                WBDNodes.Nodes.Add("Well Bore Quality ");
                WBDNodes.Nodes.Add("Casing Program Sketch ");
                this.treeView.tvMenu.Nodes.Add(WBDNodes);

                TreeNode DTSNodes = new TreeNode("  Drilling Team Select");
                this.treeView.tvMenu.Nodes.Add(DTSNodes);
                TreeNode BDNodes = new TreeNode("  BHA Design");
                this.treeView.tvMenu.Nodes.Add(BDNodes);
                TreeNode CANodes = new TreeNode("  Consumables Amount");
                CANodes.Nodes.Add("Drilling Fluid");
                CANodes.Nodes.Add("Cost Estimation");
                CANodes.Nodes.Add("Bit Types");
                this.treeView.tvMenu.Nodes.Add(CANodes);
                TreeNode RPNodes = new TreeNode("  Requirement Params ");
                RPNodes.Nodes.Add("Hydraulic Parameters");
                RPNodes.Nodes.Add("Well Controler");
                RPNodes.Nodes.Add("Casing String");
                RPNodes.Nodes.Add("Other design");
                RPNodes.Nodes.Add("Document Requirements");
                this.treeView.tvMenu.Nodes.Add(RPNodes);
                TreeNode DPPNodes = new TreeNode("  Drilling Progress Plan");
                this.treeView.tvMenu.Nodes.Add(DPPNodes);
                btnrefresh_BtnClick(null, null);
                this.treeView.tvMenu.AfterSelect += tvMenu_AfterSelect;
                AddControl(new IndexControl());
            }
            finally
            {
                ControlHelper.FreezeControl(this, false);
            }
        }
        private void panControl_ControlAdded(object sender, ControlEventArgs e)
        {
            this.panControl.VerticalScroll.Enabled = true;
            this.panControl.VerticalScroll.Visible = true;
            this.panControl.Scroll += panControl_Scroll;
        }
        /// <summary>
        /// panel控件的事件：用户或代码滚动工作时发生 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void panControl_Scroll(object sender, ScrollEventArgs e)
        {
            this.panControl.VerticalScroll.Value = e.NewValue;
        }
        //private void notifyIcon1_Click(object sender, EventArgs e)
        //{
        //    if (this.WindowState == FormWindowState.Normal)
        //    {
        //        this.notifyIcon1.Visible = true;
        //        this.Visible = false;
        //        this.ShowInTaskbar = true;
        //    }
        //}
        private void btnrefresh_BtnClick(object sender, EventArgs e)
        {
            SQLiteDataReader reader = sqliteHelper.ReadFullTable("SessionData");
            //利用魔法将(SQLite)DataReader转换为List
            projects = EntityTranslate.Translate<SessionData>(reader);
            FrmTips.ShowTipsSuccess(new FormMain(), "已更新数据库");
        }
        private void FormMain_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            try
            {
                if (FrmDialog.ShowDialog(this, "确认执行退出操作？", "操作提示", true) == DialogResult.OK)
                {
                    Process.GetCurrentProcess().Kill();
                }
                else
                {
                    e.Cancel = true;
                }
            }
            catch (Exception ex)
            {
                Process.GetCurrentProcess().Kill();
            }
        }
    }
}
